package breeze.pixel.weather.apps_util.views;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import androidx.annotation.NonNull;

import java.nio.charset.StandardCharsets;

import breeze.pixel.weather.R;
import brz.breeze.app_utils.BActivity;

public class WebActivity extends BActivity {

    private WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web);
        init();
    }

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    public void init() {
        //实现状态栏沉浸
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
        getWindow().setStatusBarColor(Color.WHITE);
        webView = find(R.id.webActivity_WebView);
        //init the webView settings.
        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
        settings.setLoadsImagesAutomatically(true);
        settings.setSupportZoom(true);
        settings.setBlockNetworkLoads(false);
        settings.setDefaultTextEncodingName(String.valueOf(StandardCharsets.UTF_8));
        webView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (url.startsWith("http")){
                    view.loadUrl(url);
                    return true;
                }else {
                    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                    startActivity(intent);
                    Handler handler = new Handler(Looper.myLooper()){
                        @Override
                        public void handleMessage(@NonNull Message msg) {
                            super.handleMessage(msg);
                            finish();
                        }
                    };
                    handler.sendEmptyMessageDelayed(0,2000);
                }
                return false;
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        initData();
    }

    @Override
    public void initData() {
        //Check weather there are data in the intent
        String uri = getIntent().getStringExtra("uri");
        if (null!=uri&&!uri.isEmpty()){
            loadUri(uri);
        }
        String htmlCode = getIntent().getStringExtra("htmlCode");
        if (null!=htmlCode&&!htmlCode.isEmpty()){
            loadHtml(htmlCode);
        }
    }

    private void loadUri(String uri){
        webView.loadUrl(uri);
    }

    private void loadHtml(String htmlCode){
        webView.loadData(htmlCode,"text/html", String.valueOf(StandardCharsets.UTF_8));
    }

    public static void startActivity(Context context,String url){
        Intent intent = new Intent(context,WebActivity.class);
        intent.putExtra("uri",url);
        context.startActivity(intent);
    }

    public static void startActivityWithHTML(Context context, String html){
        Intent intent = new Intent(context,WebActivity.class);
        intent.putExtra("htmlCode",html);
        context.startActivity(intent);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK){
            if (webView.canGoBack()){
                webView.goBack();
                return false;
            }else {
                finish();
            }
        }
        return true;
    }
}